package com.huangssssx.loader
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	public class ImageLoader extends AbstractLoader implements ILoader
	{
		public function ImageLoader()
		{
		}
		
		public function load(imageName:String, backFun:Function):void
		{
			
			var image:Bitmap = ResManager.getInstance().getRes(ResManager.RESOURCE_IMAGE,imageName) as Bitmap;
			if(image)
			{
				backFun();
				return;
			}
			
			var loader:Loader = new Loader();
			var path:String = (ResManager.getInstance().getRes(ResManager.RESOURCE_CONFIG,"resRoot") as String)+imageName;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadCompleted(imageName,backFun));
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onErrorHandler);
			loader.load(new URLRequest(path));
		}
		
		private function onLoadCompleted(imageName:String,backFun:Function):Function
		{
			var fun:Function = function(event:Event):void
			{
				var targetLoaderInfo:LoaderInfo = event.target as LoaderInfo;
				targetLoaderInfo.removeEventListener(Event.COMPLETE,arguments.callee);
				ResManager.getInstance().setRes(ResManager.RESOURCE_IMAGE,imageName, targetLoaderInfo.content);
				backFun();
			};
			
			return fun;
		}
			
	}
}